package com.ctl;

import com.ctl.starter.all.CustomResourceLoader;
import com.ctl.starter.all.CustomerConfigurableApplicationContext;
import com.ctl.starter.all.SafeBeanNameGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
@Slf4j
@SpringBootApplication
public class StarterApplication {

	private static ConfigurableApplicationContext configurableApplicationContext = null;
	public static ConfigurableApplicationContext getConfigurableApplicationContext() {
		return configurableApplicationContext;
	}

	public static void setConfigurableApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
		StarterApplication.configurableApplicationContext = configurableApplicationContext;
	}

	public static void main(String[] args) {
		SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder()
				.sources(StarterApplication.class)
				.bannerMode(Banner.Mode.OFF)
				.web(WebApplicationType.SERVLET)
				.resourceLoader(new CustomResourceLoader(new DefaultResourceLoader()))
				.beanNameGenerator(new SafeBeanNameGenerator())
				.initializers(new CustomerConfigurableApplicationContext());
		ConfigurableApplicationContext configurableApplicationContext = springApplicationBuilder.run(args);
		setConfigurableApplicationContext(configurableApplicationContext);
		log.info("######AllApplication.run############");
	}

	// 如果你要打包成 war 包部署到外部 Tomcat，需要这个类
	public static class ServletInitializer extends SpringBootServletInitializer {
		@Override
		protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
			return builder.sources(StarterApplication.class);
		}
	}
}
